package com.bookcycle.panda.webfront.controller;


import java.util.HashMap;
import java.util.Map;

import com.baomidou.kisso.security.token.SSOToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import com.baomidou.kisso.SSOHelper;
import com.bookcycle.panda.webfront.constant.URLConstant;
import com.bookcycle.panda.webfront.vo.House;
import com.bookcycle.panda.webfront.vo.Order;


/**
 * @author 张澧枫
 * @data   2018-3-20下午5:14:14
 * @version 
 */
@Controller
public class CoController extends BaseController{
	
	private final Logger logger = LoggerFactory.getLogger(CoController.class);
	
	/**
	 * 
	 * @Title: corequest   
	 * @Description: TODO(调用退房微服务的接口)   
	 * @param orderId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/corequest" , method = RequestMethod.GET)
	public String corequest(@RequestParam("orderId")String orderId,Model model){
		/**
		 * 根据请求返回过来的参数判断应该显示退房成功还是失败页面
		 */
		//String oid="2018031408533671A26704215A4C27940AADFB6E61A47269703166";
		Map<String, String> orderIdMap = new HashMap<>();
		orderIdMap.put("orderId", orderId);
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity<Order> Entity = restTemplate.getForEntity(URLConstant.URL_CHECKOUT_REQUEST+"{oId}", Order.class,orderId);
		
		model.addAttribute("orders",Entity.getBody());
		//根据返回的订单状态码跳转到不同的两个页面（7.退房成功8.退房逾期）
		if(7 == (Entity.getBody().getOrderStatus())){
			return "cosuccess";
		}else if(8 == (Entity.getBody().getOrderStatus())){
			return "coexpire";
		}
		logger.info("orderId="+orderId);
		return "coexpire";
	}
	
	/**
	 * 
	 * @Title: checkout   
	 * @Description: 调用退房时间逾期页面  
	 * @return  
	 */
    @RequestMapping("/coexpire")
    public String checkout(){
    	return "coexpire";
    }
    
    /**
     * 
     * @Title: cosuccess   
     * @Description: 调取退房成功的页面   
     * @return
     */
    @RequestMapping("/cosuccess")
    public String cosuccess(){
    	return "cosuccess";
    }
    
    /**
     * 
     * @Title: order   
     * @Description: TODO(调用下单服务的接口)   
     * @param houseId
     * @return
     */
    @RequestMapping(value = "/order",method = RequestMethod.POST)
    public String order(@RequestParam("houseId")String houseId,Model model){
		SSOToken ssoToken=SSOHelper.getSSOToken(request);
		if(ssoToken==null){
			return "redirect:signIn";
		}
	 	String userId=SSOHelper.getSSOToken(request).getId();
	 	RestTemplate restTemplate = new RestTemplate();
	 	Map<String,String> map=new HashMap<String,String>();
	 	map.put("id",houseId);
	 	ResponseEntity<House> entity = restTemplate.getForEntity(URLConstant.URL_GET_HOUSE, House.class,map);
	 	model.addAttribute("houseList",entity.getBody());
	 	model.addAttribute("userId",userId);
	 	return "order";
    }
}
